package com.zhuss.team.processor;

import com.zhuss.bean.bo.team.answer.CreateAnswerBo;
import com.zhuss.bean.unit.team.AnswerBean;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.PublishUserStatus;
import com.zhuss.exception.WpsException;
import com.zhuss.team.db.dao.ext.InvestAnswerDao;
import com.zhuss.team.db.dao.ext.InvestPublishUserDao;
import com.zhuss.team.db.entity.InvestPublishUser;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/3 14:32
 */
@Component("answerCreate")
public class AnswerCreate extends AbstractProcessor<Object, CreateAnswerBo> implements Processor<Object, CreateAnswerBo> {

    @Autowired
    private InvestAnswerDao answerDao;

    @Autowired
    private InvestPublishUserDao investPublishUserDao;

    @Override
    public Object process(CreateAnswerBo bo) throws WpsException {
        AnswerBean ab = new AnswerBean();
        ab.setPublishUserId(bo.getAnswer().getPublishUserId());
        ab.setPublishId(bo.getAnswer().getPublishId());
        ab.setInvestId(bo.getAnswer().getInvestId());
        ab.setInvestName(bo.getAnswer().getInvestName());
        ab.setQuestionList(bo.getAnswer().getQuestionList());
        ab.setArea(bo.getConcurrentArea());
        ab.setCreateId(bo.getConcurrentUserId());
        ab.setCreateName(bo.getConcurrentUserName());
        ab.setCreateTime(new Date());
        answerDao.createBatch(ab);

        InvestPublishUser investPublishUser = new InvestPublishUser();
        investPublishUser.setPublishUserId(bo.getAnswer().getPublishUserId());
        investPublishUser.setStatus(PublishUserStatus.已填写调查.key());

        investPublishUser.setModifierId(bo.getConcurrentUserId());
        investPublishUser.setModifier(bo.getConcurrentUserName());
        investPublishUser.setModifyTime(new Date());
        investPublishUserDao.updateByPrimaryKeySelective(investPublishUser);
        return null;
    }

    @Override
    public Object log(CreateAnswerBo param) throws WpsException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.协同发布.key());
        record.setAction(LogActionEnum.保存问卷.key());
        record.setTargetId(param.getTargetId());
        record.setTargetName(param.getAnswer().getInvestName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public void check(CreateAnswerBo bo) throws WpsException {

    }
}
